var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    var that = this
    //从福利产品订单进到支付
    if (options.state=='order'){
      wx.getStorage({
        key: 'openid',
        success: function (res) {
          let openid = res.data
          wx.showLoading({
            title: '加载中',
          })
          wx.request({
            url: app.globalData.URL + '/app/personalOrder/placeOrder?openid=' + openid + '&id=' + options.id,
            success: function (res) {
              wx.hideLoading()
              console.log('personalOrder/placeOrder', res.data)
              that.setData({
                orderNum: res.data.data.orderNumber,
                money: res.data.data.orderMoney,
                time: res.data.data.createDate,
                orderId: res.data.data.orderId,
                timeStamp: res.data.timeStamp,
                nonceStr: res.data.nonceStr,
                package: res.data.packageStr,
                signType: res.data.signType,
                paySign: res.data.paySign,
                module: res.data.module,
                payType: res.data.payType,
                payMoney: res.data.payMoney
              })
            }
          })
        },
      })
      //秒杀
    } else if (options.state == "3"){
      wx.getStorage({
        key: 'openid',
        success: function (res) {
          let openid = res.data
          wx.showLoading({
            title: '加载中',
          })
          wx.request({
            url: app.globalData.URL + '/app/seckill/placeOrder?openid=' + openid + '&id=' + options.ids + '&deliveryId=' + options.deliveryId + '&msg=' + options.msg + '&money=' + options.money + '&num=' + options.number,
            success: function (res) {
              wx.hideLoading()
              console.log('seckill/placeOrder', res.data)
              that.setData({
                orderNum: res.data.data.orderNumber,
                money: res.data.data.orderMoney,
                time: res.data.data.createDate,
                orderId: res.data.data.orderId,
                timeStamp: res.data.timeStamp,
                nonceStr: res.data.nonceStr,
                package: res.data.packageStr,
                signType: res.data.signType,
                paySign: res.data.paySign,
                module: res.data.module,
                payType: res.data.payType,
                payMoney: res.data.payMoney
              })
            }
          })
        },
      })
      //团购
    } else if (options.state == "4") {
      wx.getStorage({
        key: 'openid',
        success: function (res) {
          let openid = res.data
          wx.showLoading({
            title: '加载中',
          })
          wx.request({
            url: app.globalData.URL + '/app/seckill/placeOrder2?openid=' + openid + '&id=' + options.ids + '&deliveryId=' + options.deliveryId + '&msg=' + options.msg + '&money=' + options.money + '&num=' + options.number,
            success: function (res) {
              wx.hideLoading()
              console.log('seckill/placeOrder2', res.data)
              that.setData({
                orderNum: res.data.data.orderNumber,
                money: res.data.data.orderMoney,
                time: res.data.data.createDate,
                orderId: res.data.data.orderId,
                timeStamp: res.data.timeStamp,
                nonceStr: res.data.nonceStr,
                package: res.data.packageStr,
                signType: res.data.signType,
                paySign: res.data.paySign,
                module: res.data.module,
                payType: res.data.payType,
                payMoney: res.data.payMoney
              })
            }
          })
        },
      })
    }else{
      wx.getStorage({
        key: 'openid',
        success: function (res) {
          let openid = res.data
          wx.showLoading({
            title: '加载中',
          })
          wx.request({
            url: app.globalData.URL + '/app/product/placeOrder?openid=' + openid + '&ids=' + options.ids + '&money=' + options.money + '&number=' + options.number + '&deliveryId=' + options.deliveryId + '&type=' + options.type + '&expenses=0' + '&msg=' + options.msg,
            success: function (res) {
              wx.hideLoading()
              console.log('product/placeOrder', res.data)
              that.setData({
                orderNum: res.data.data.orderNumber,
                money: res.data.data.orderMoney,
                time: res.data.data.createDate,
                orderId: res.data.data.orderId,
                timeStamp: res.data.timeStamp,
                nonceStr: res.data.nonceStr,
                package: res.data.packageStr,
                signType: res.data.signType,
                paySign: res.data.paySign,
                module: res.data.module,
                payType: res.data.payType,
                payMoney: res.data.payMoney
              })
            }
          })
        },
      })
    }
    
  },


  wxPay:function(){
    var that = this
    
    var openid = wx.getStorageSync('openid')
    var notifyParam =
    {
      'orderId': that.data.orderId, 'money': that.data.payMoney
    }
    var param = {
      'openId': openid,
		  'payWay': 'WECHAT',
		  'module': that.data.module,
      'merOrderNo': that.data.orderNum,
		  'payType': that.data.payType,
      'notifyParam': notifyParam
    }

    console.log("参数:", param)
    wx.request({
      url: app.globalData.URL + '/app/order1/pay',
      method: 'post',
      data: JSON.stringify(param),
      header: {
        "Content-Type": "application/json;charset=utf-8"
      },
      success: function (res) {
        console.log(res.data)
        if(res.data.flag == true){
          wx.requestPayment({
            timeStamp: that.data.timeStamp,
            nonceStr: that.data.nonceStr,
            package: that.data.package,
            signType: that.data.signType,
            paySign: that.data.paySign,
            success(res) {
              console.log("success", res)

              wx.navigateTo({
                url: '../welfareOrder/welfareOrder'
              })
            },
            fail(res) {
              console.log("fail", res)
            }
          })
        }
      }
    })        
  },

  ooPay:function(){
    var that = this
    var openid = wx.getStorageSync('openid')
    var notifyParam =
    {
      'orderId': that.data.orderId, 'money': that.data.payMoney
    }
    var param = {
      'openId': openid,
      'payWay': 'WECHAT',
      'module': that.data.module,
      'merOrderNo': that.data.orderNum,
      'payType': that.data.payType,
      'notifyParam': notifyParam
    }

    console.log("参数:", param)
    wx.request({
      url: app.globalData.URL + '/app/order1/pay',
      method: 'post',
      data: JSON.stringify(param),
      header: {
        "Content-Type": "application/json;charset=utf-8"
      },
      success: function (res) {
        console.log("order/pay",res.data)
        if (res.data.flag == true) {
          
          wx.request({
            url: app.globalData.URL + '/app/order1/pay?openId=' + openid + '&payWay=OOPAY&module=' + that.data.module + '&merOrderNo=' + that.data.orderNum + '&payType='+ that.data.payType + '&payMoney=' + that.data.payMoney,
            success:function(res){
              console.log("order/pay",res.data)
              
              if(res.data.flag == false){
                wx.showModal({
                  title: '',
                  content: res.data.msg,
                  success: function (res) {
                    if (res.cancel) {
                      wx.navigateBack({})
                    } else {
                      wx.navigateBack({})
                    }
                  },
                })
              }else{
                wx.showModal({
                  title: '',
                  content: '支付成功',
                  success: function (res) {
                    if (res.cancel) {
                      wx.navigateBack({})
                    } else {
                      wx.navigateBack({})
                    }
                  }
                })
              }
            }
          })
        }
      }
    })        
  }
})